package com.paradise.phone;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.paradise.phone.aidl.IKeepLiveInterface;

public class KeepLiveService extends Service {

    private KeepLiveBinder klBinder;
    private KeepLiveConnection fphoneConnection;

    public KeepLiveService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return klBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        klBinder = new KeepLiveBinder();
        fphoneConnection = new KeepLiveConnection();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        this.bindService(new Intent(this, FacephoneService.class),
                fphoneConnection, BIND_IMPORTANT);
    }

    public class KeepLiveBinder extends IKeepLiveInterface.Stub {

        @Override
        public String getServiceName() throws RemoteException {
            return "KeepLiveService";
        }
    }

    public class KeepLiveConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("flag->", "KeepLiveService.java(onServiceConnected:35)->"+"Facephone后台进程连接成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("flag->", "FacephoneService.java(onServiceDisconnected:106)->"+"Facephone后台进程被杀死");
            KeepLiveService.this.startService(new Intent(KeepLiveService.this, FacephoneService.class));
            KeepLiveService.this.bindService(new Intent(KeepLiveService.this, FacephoneService.class),
                    fphoneConnection, BIND_IMPORTANT);
        }
    }
}
